﻿/* 
 *  <copyright file="ToggleMoverActiveCmp.cs" company="Wesley Jowitt">
 *  
 *  SobrietyEngine: Copyright (c) 2011 Wesley Jowitt
 *  
 *  This software is distributed under a software license.
 *   * As of the creation time of this file, the license in use is the Microsoft Permissive License (Ms-PL) 
 *    * The license can be viewed at:
 *     * License.Txt in the root folder of this project
 *      * http://sobrietyengine.codeplex.com/license
 *       * http://www.microsoft.com/opensource/licenses.mspx
 *       
 *  </copyright>
 *   */

using System;
using System.Collections.Generic;

using SobrietyEngine.Guids;
using SobrietyEngine.Actor;

namespace SobrietyEngine.Components.Trigger.Response
{
    public class ToggleMoverActiveCmp:TriggerResponseBase
    {
        IMovement mover;
        IFarseerPhysicsBody physicsBody;
        Boolean stopFarseer;

        public ToggleMoverActiveCmp()
            : base()
        {
        }

        public ToggleMoverActiveCmp(String[] triggerActor, Boolean stopFarseerBody)
            : base(triggerActor)
        {
            stopFarseer = stopFarseerBody;
        }

        override public Boolean InitComponent()
        {
            base.InitComponent();

            mover = Actor.Get<IMovement>();
            if (mover == null)
            {
                Logging.Logger.Error(this, "Couldnt find IMover providing component on parent actor");                
            }

            if (stopFarseer)
            {
                physicsBody = Actor.Get<IFarseerPhysicsBody>();
                if (physicsBody == null)
                    Logging.Logger.Error(this, "COuldnt find provider of IFarseerPhysicsBody");
            }

            return true;
        }

        public override void Trigger(ITrigger trigger, CGuid byWho)
        {
            IActor trigAct = ((IComponent)trigger).Actor;
            ITrigger trig = trigAct.Get<ITrigger>();
            ((IComponent)mover).Active = trig.TriggerValue;
            if (trig.TriggerValue == false && stopFarseer == true)
                physicsBody.Body.ResetDynamics();
        }

        public  override void Trigger(CGuid byWho)
        {
            ((IComponent)mover).Active = !((IComponent)mover).Active;
            if (stopFarseer == true && ((IComponent)mover).Active == false)
                physicsBody.Body.ResetDynamics();
        }
    }
}
